﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

using Newtonsoft.Json;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.MyActionResult;
using Common;
using CashFlow.ShopManage.Def;
using System.Web.DynamicData;

namespace CashFlow.ShopManage.Common
{
    public class MyCommonTool
    {
        public static bool IsRootUser(ShopInfoEntity ui)
        {
            return ui.Id == MyConfig.RootUserId;
        }

        public static string GetOrderDate()
        {
            DateTime date = DateTime.Now;

            if (date.Hour < 15)
            {
                date = date.AddDays(-1);
            }

            return date.ToString("yyyy-MM-dd");
        }

        public static string GetShopLevelName(int orderCount)
        {
            if (orderCount >= (1 + 30))
            {
                return "直营店铺";
            }
            else if (orderCount >= (1 + 20))
            {
                return "旗舰店铺";
            }
            else if (orderCount >= (1 + 10))
            {
                return "高级店铺";
            }
            else if (orderCount >= (1 + 5))
            {
                return "中级店铺";
            }
            else if (orderCount >= (1 + 1))
            {
                return "初级店铺";
            }
            else
            {
                return "--";
            }
        }

        public static bool IsIllegalShopManagerLevelPercent(decimal percent)
        {
            if (percent == Consts.ShopManagerLevelPercent_Junior
                || percent == Consts.ShopManagerLevelPercent_Intermidate
                || percent == Consts.ShopManagerLevelPercent_Senior
                || percent == Consts.ShopManagerLevelPercent_FlagShip
                || percent == Consts.ShopManagerLevelPercent_DirectSale)
            {
                return true;
            }

            return false;
        }

        public static bool ValidateOrderAmount(decimal amount)
        {
            if (amount >= MyConfig.OrderAmountMin
                && amount <= MyConfig.OrderAmountMax)
            {
                return true;
            }
            return false;
        }

        public static bool ValidateOrderState(int state)
        {
            if (state == (int)EOrderState.NotPayment
                || state == (int)EOrderState.Paied
                || state == (int)EOrderState.Recv
                || state == (int)EOrderState.Extract)
            {
                return true;
            }

            return false;
        }

        public static ActionResult CreateOperationResult(
            bool success
            , string msg
            , object data)
        {
            var obj = new
            {
                Success = success,
                Msg = msg,
                Data = data
            };
            return new JsonResult
            {
                Data = obj,
                ContentType = "application/json",
                ContentEncoding = Encoding.UTF8
            };
        }

        public static ActionResult CreateLayUITableResult(
            bool success
            , string msg
            , object data
            , int totalCount = 0)
        {
            var obj = new
            {
                Success = success,
                Msg = msg,
                Data = data,
                Total = totalCount
            };
            return new MyJsonResult(obj);
        }

        public static string CreateOrderNo()
        {
            return string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff"));
        }

        public static string GetProductName()
        {
            int index = RandTool.CreateRandValWithMinMax(0,
                MyConfig.ProductNames.Count - 1);
            return MyConfig.ProductNames.ElementAt(index);
        }
    }
}